/**
 * Created by wwh on 2017/7/29.
 */
var map = new BMap.Map("allmap");
var local = new BMap.LocalSearch(map, {
    renderOptions:{map: map}
});
layui.use(['form','upload','jquery','layer'],function () {
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer,
        image = {};

    var vm = new Vue({
        el:'#content'
        ,data:{
            orgInfo:{}
        }
        ,methods: {
            /**
             * 获取信息
             */
            getInfo:function () {
                $('#reload').attr("href",origin);
                // 清空图片脏数据
                $('#business').attr('src', "");
                $('#out').attr('src', "");
                // 操作类型 1-添加 2-查看详情 3-编辑
                if(operateType !== 1) {
                    axios.get('/orginfo/findById/' + orgId)
                        .then(function (response) {
                            console.log(response.data);
                            vm.orgInfo = response.data.data.orgInfo;
                            vm.getMapInfo();
                            console.log(vm.orgInfo);
                            //改变图片显示
                            if (response.data.data.businessliscense !== null)
                                $('#business').attr('src', response.data.data.businessliscense.path);
                            if (response.data.data.outsidepic !== null)
                                $('#out').attr('src', response.data.data.outsidepic.path);
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                    if (operateType === 3) {  // 编辑
                        $('#content input[type=file]').show(); // layui中input[type=file]的缘故
                        $('#content input[type!=button]').attr("readOnly", false).removeClass("disabled");
                        $('#content input[value="提交审核"]').show();
                        $('#content input[value="暂时保存"]').show();
                    }
                    else if(operateType === 2) {  // 查看详情
                        $('#content input[type=file]').remove(); // layui中input[type=file]的缘故
                        $('#content input[type!=button]').attr("readOnly", true).addClass("disabled");
                        $('#content input[value="提交审核"]').hide();
                        $('#content input[value="暂时保存"]').hide();
                    }
                }
                else{
                    $('#content input[type=file]').parent().parent().show(); // layui中input[type=file]的缘故
                    $('#content input[type!=button]').attr("readOnly", false).removeClass("disabled");
                    $('#content input[value="提交审核"]').show();
                    $('#content input[value="暂时保存"]').show();
                    vm.getMapInfo();
                }
            },
            /**
             * 地图信息
             */
            getMapInfo: function () {
                var point = new BMap.Point(vm.orgInfo.longtitude, vm.orgInfo.latitude);

                if(vm.orgInfo.longtitude != null&&vm.orgInfo.latitude != null) {
                    map.centerAndZoom(point,14);
                    map.clearOverlays();
                    var marker = new BMap.Marker(point);  // 创建标注
                    map.addOverlay(marker);               // 将标注添加到地图中
                    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                }
                else{
                    map.centerAndZoom("中国",14);
                }

                //单击获取点击的经纬度并添加跳动动画标注
                map.addEventListener("click",function(e){
                    map.clearOverlays();
                    vm.orgInfo.longtitude = e.point.lng;
                    vm.orgInfo.latitude = e.point.lat;
                    $('#longtitude').val(vm.orgInfo.longtitude);
                    $('#latitude').val(vm.orgInfo.latitude);

                    var point = new BMap.Point(vm.orgInfo.longtitude, vm.orgInfo.latitude);
                    var marker = new BMap.Marker(point);  // 创建标注
                    map.addOverlay(marker);               // 将标注添加到地图中
                    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
                });
                map.setDefaultCursor("url('bird.cur')");   //设置地图默认的鼠标指针样式
                map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
                map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用

            },
            /**
             * 保存功能（提交审核或暂时保存）
             */
            save: function () {
                var url = vm.orgInfo.id == null ? "/orginfo/addOne" : "/orginfo/modifyOneById";
                vm.orgInfo.creatorid = userid;
//                    console.log(url);
//                    console.log(vm.orgInfo);
                axios.post(url,vm.orgInfo)
                    .then(function(response){
//                            console.log(response);
                        if (response.data.success)
                            layer.msg("保存成功", {icon: 1, time: 1000}, function () {
                                vm.reload();
                            });
                        else{
                            layer.msg("保存失败", {icon: 2, time: 1000});
                        }
                    })
                    .catch(function (response) {
                        console.log(response);
                    });
            },
            reload:function () {
                location.href = origin;
            }
        }
    });
    vm.getInfo();

    // 地图查询监听
    var btn = document.getElementById('address');
    btn.addEventListener("click",function () {
        var address = $("#suggestId").val();
        // console.log(address);
        local.search(address);
    });

    //营业执照
    layui.upload({
        url: '/upload/businessliscense',
        elem: '#businessliscense',
        success: function (response) {
            image.path = response.data;
            image.description = "营业执照";
            image.isencrypted = 1;//需要加密
            axios.post("/image/add", image)
                .then(function (response) {
                    // console.log(response.data);
                    vm.orgInfo.businessliscense = response.data.data;
//                        console.log(vm.orgInfo);
                    axios.get("/image/find/" + response.data.data)
                        .then(function (response) {
//                                console.log(response.data);
                            $('#business').attr('src', response.data.data);
                        })
                        .catch(function (response) {
                            console.log(response);
                        });
                })
                .catch(function (response) {
                    console.log(response);
                })

        },
        before: function () {
            console.log('营业执照上传中');
        }
    });
    //单位外景
    layui.upload({
        url: '/upload/out',
        elem: '#outsidepic',
        success: function (response) {
            $('#out').attr('src', response.data);
//                console.log(response.data);
            image.path = response.data;
            image.description = "单位外景";
            image.isencrypted = 2;//不需要加密
            axios.post("/image/add", image)
                .then(function (response) {
                    // console.log(response.data);
                    vm.orgInfo.outsidepic = response.data.data;
//                        console.log(vm.orgInfo);
                })
                .catch(function (response) {
                    console.log(response);
                });
        },
        before: function () {
            console.log('outsidepic文件上传中');
        }
    });

    // 监听提交审核按钮
    form.on('submit(save)',function () {
        vm.orgInfo.state="2";
        vm.save();
        return false;
    });
    // 监听暂时保存
    form.on('submit(saveTemporary)',function () {
        vm.orgInfo.state="1";
        vm.save();
        return false;
    });

    $('#longtitude').keyup(function () {
        vm.orgInfo.longtitude = $('#longtitude').val();
        console.log(vm.orgInfo.longtitude);
        vm.getMapInfo();
    });
    $('#latitude').keyup(function () {
        vm.orgInfo.latitude = $('#latitude').val();
        console.log(vm.orgInfo.latitude);
        vm.getMapInfo();
    })
});